import 'dart:convert';

import 'package:df/utils/logger_utils.dart';

//使用Dart Data Class Generator插件进行创建
//使用命令: Generate from JSON

// {
//   "Code": 0,
//   "Msg": "",
//   "UpdateStatus": 1,
//   "VersionCode": 3,
//   "VersionName": "1.0.2",
//   "UploadTime": "2018-07-10 17:28:41",
//   "ModifyContent": "\r\n1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。",
//   "DownloadUrl": "https://xuexiangjys.oss-cn-shanghai.aliyuncs.com/apk/xupdate_demo_1.0.2.apk",
//   "ApkSize": 2048,
//   "ApkMd5": "E4B79A36EFB9F17DF7E3BB161F9BCFD8"
// }

class UpdateApp {
  final int resultCode;
  final String resultMsg;
  final int queryCount;
  final dynamic resultMsgEx;
  final AppData data;

  UpdateApp({
    required this.resultCode,
    required this.resultMsg,
    required this.queryCount,
    required this.resultMsgEx,
    required this.data,
  });


  static UpdateApp? fromJsonString(String source) {
    try {
      final Map<String, dynamic> jsonMap = json.decode(source);
      return UpdateApp.fromJson(jsonMap);
    } catch (e) {
      LogUtils.e('【反序列化】 出错= $e');
      return null;
    }
  }

  factory UpdateApp.fromJson(Map<String, dynamic> json) {
    return UpdateApp(
      resultCode: json['resultCode'] ?? 0,
      resultMsg: json['resultMsg'] ?? "",
      queryCount: json['queryCount'] ?? 0,
      resultMsgEx: json['resultMsgEx'],
      data: AppData.fromJson(json['data']),
    );
  }
}

class AppData {
  final AndroidInfo android;

  AppData({required this.android});

  factory AppData.fromJson(Map<String, dynamic> json) {
    return AppData(
      android: AndroidInfo.fromJson(json['android']),
    );
  }
}

class AndroidInfo {
  final int newVersionCode;
  final String newVersionName;
  final bool isUpdate;
  final bool forceUpdate;
  final String apkUrl;
  final String releaseDate;
  final int apkSize;
  final String md5;
  final ReleaseNotes releaseNotes;

  AndroidInfo({
    required this.newVersionCode,
    required this.newVersionName,
    required this.isUpdate,
    required this.forceUpdate,
    required this.apkUrl,
    required this.releaseDate,
    required this.apkSize,
    required this.md5,
    required this.releaseNotes,
  });

  factory AndroidInfo.fromJson(Map<String, dynamic> json) {
    return AndroidInfo(
      newVersionCode: json['newVersionCode'] ?? 0,
      newVersionName: json['newVersionName'] ?? "",
      isUpdate: json['isUpdate'] ?? false,
      forceUpdate: json['forceUpdate'] ?? false,
      apkUrl: json['apkUrl'] ?? "",
      releaseDate: json['releaseDate'] ?? "",
      apkSize: json['apkSize'] ?? 0,
      md5: json['md5'] ?? "",
      releaseNotes: ReleaseNotes.fromJson(json['releaseNotes']),
    );
  }
}

class ReleaseNotes {
  final String en;
  final String es;
  final String zh;
  final String vi;
  final String km;

  ReleaseNotes({
    required this.en,
    required this.es,
    required this.zh,
    required this.vi,
    required this.km,
  });

  factory ReleaseNotes.fromJson(Map<String, dynamic> json) {
    return ReleaseNotes(
      en: json['en'] ?? "",
      es: json['es'] ?? "",
      zh: json['zh'] ?? "",
      vi: json['vi'] ?? "",
      km: json['km'] ?? "",
    );
  }
}


